package com.diligreat.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.diligreat.mapper.ContentMapper;
import com.diligreat.mapper.LangMapper;
import com.diligreat.mapper.SelectionMapper;
import com.diligreat.domain.Content;
import com.diligreat.domain.Lang;
import com.diligreat.domain.Selection;

@Service
public class IndexService {
	
	@Autowired
	private ContentMapper cm;
	@Autowired
	private SelectionMapper sm;
	
	
	@Autowired
	private LangMapper lm;
	
	public List<Content> getTenContentList()
	{
		return cm.getTenContent();
	}
	
	/**
	 * 首页获得首页文章
	 */
	public List<Content> getTenContentList(long langid)
	{
		return this.cm.getTenContentByLang(langid);
	}
	
	
	/**
	 * 第一个参数是语言分类, 第二个是具体分类
	 * @param lang
	 * @param cataid
	 * @return
	 */
	public List<Selection> getSelection(long lang,long cataid)
	{
		Selection s=new Selection();
		s.setLang(lang);
		s.setCataid(cataid);
		
		return sm.getSelection(s);
	}
	
	
	/**
	 * 获得一个语言的全体属性
	 * @param lang
	 * @return
	 */
	public List<Selection> getAllSelectionByOrder(long lang)
	{
		Selection s=new Selection();
		s.setLang(lang);
		s.setCataid(0);
		
		List<Selection> l=sm.getSelection(s);
		
		
		
		for(int i=0;i<l.size();i++)
		{
			Selection tmp=(Selection)l.get(i);
			s.setCataid(tmp.getId());

			tmp.setSubselection(sm.getSelection(s));
		}
		
		return l;
	}
	
	
	/**
	 * 获得全体SELECTION选项列表
	 * @return
	 */
	public List<Selection> getAllSelectionByOrder()
	{
		Selection s=new Selection();
		s.setCataid(0);
		
		List<Selection> l=sm.getAllSelection(s);
		
		
		
		for(int i=0;i<l.size();i++)
		{
			Selection tmp=(Selection)l.get(i);
			s.setCataid(tmp.getId());

			tmp.setSubselection(sm.getSelection(s));
		}
		
		return l;
	}
	
	/***
	 * 根据提交过来的序号获得是那种语言
	 * @param id
	 * @return
	 */
	public Lang getLangById(long id)
	{
		List<Lang> list=lm.getLangById(id);
		
		
		Lang l=null;
		if(list.size()>0)
		{
			l=(Lang)list.get(0);
		}
		
		
		return l;
	}
	
	
	/**
	 * 使用ID获得文章
	 * @param id
	 * @return
	 */
	public Content getContentById(long id)
	{
		return cm.getContentById(id);
	}
	
	/**
	 * 获得带二级分类的语言表单
	 * @return
	 */
	public List<Lang> getGroupLang()
	{
		List<Lang> l=this.lm.getLangGroupById(0);
		
		for(int i=0;i<l.size();i++)
		{
			Lang lang=(Lang)l.get(i);
			
			lang.setUnderlist(this.lm.getLangGroupById(lang.getId()));
		}
		
		return l;
	}
	
	
	public List<Lang> getLangResource()
	{
		List<Lang> l=this.lm.getLangGroupById(0);
		
		for(int i=0;i<l.size();i++)
		{
			Lang tmp=(Lang)l.get(i);
			
			List<Lang> ls=this.lm.getLangGroupById(tmp.getId());
			
			
			for(int j=0;j<ls.size();j++)
			{
				Lang tmpsec=(Lang)ls.get(j);
				
				
				tmpsec.setTop(this.cm.getLabelTopContentByLang(tmpsec.getId()));
				tmpsec.setBottom(this.cm.getLabelBotContentByLang(tmpsec.getId()));
				
			}
			
			
			tmp.setUnderlist(ls);
		}
		
		
		return l;
	}
	
	
	public void insertTopLang(String name,long underid,String fl) {
		Lang l=new Lang();
		l.setId(0);
		l.setUnder_id(underid);
		l.setName(name);
		l.setFl(fl);
		
		this.lm.insertLang(l);
	}

	
	/**
	 * 获得共有多少文章
	 * @param langid
	 * @param underid
	 * @param uuId
	 * @return
	 */
	public long getTotalArtcles(long langid,long underid,long uuId)
	{
		Content c=new Content();
		
		c.setLangId(langid);
		c.setTypeId(6);
		c.setUnderId(underid);
		c.setUuId(uuId);
		
		return cm.getTotalNum(c);
	}
	
	
	/**
	 * 获得应该是多少页
	 * @param langid
	 * @param underid
	 * @return
	 */
	public long getTotalPages(long langid,long underid,long uuid)
	{
		Content c=new Content();
		
		c.setLangId(langid);
		c.setTypeId(6);
		c.setUnderId(underid);
		c.setUuId(uuid);
		
		
		long p=cm.getTotalNum(c);
		
		
		long pnd=p%c.pageeachnum;
		
		long pn=0;
		
		if(pnd>0)
		{
			pn=(p/c.pageeachnum)+1;
		}
		else
		{
			pn=p/c.pageeachnum;
		}
		
		return pn;
	}
	
	/**
	 * 读取固定的文章数目
	 * @param langid
	 * @param underid
	 * @param uuid
	 * @param startpage
	 * @return
	 */
	public List<Content> getArticles(long langid,long underid, long uuid, long startpage,String[] sl,long[] vt)
	{
		Content c=new Content();
		
		c.setLangId(langid);
		c.setTypeId(6);
		c.setUnderId(underid);
		c.setUuId(uuid);
		c.setPagefnum((startpage-1)*c.pageeachnum);
		c.setPagetnum(startpage*c.pageeachnum);

		c.setVpro(sl);
		if(vt!=null)
		{
			if(vt.length!=0)
			{
				c.setStartime(vt[0]);
				c.setEndtime(vt[0]+31536000000L*(long)vt.length);
			}
			
		}
		
		
		return cm.getLimitedArticles(c);
	}
	
	/**
	 * 用户中心中所使用的
	 * @param langid
	 * @param startpage
	 * @param sl
	 * @return
	 */
	public List<Content> getMyStuff(long langid,long startpage,String sl)
	{
		Content c=new Content();
		c.setLangId(langid);
		c.setPagefnum((startpage-1)*c.pageeachnum);
		c.setPagetnum(startpage*c.pageeachnum);
		
		String[] t=sl.split(",");
		
		c.setVpro(t);
		
		return cm.getLimitedArticles(c);
	}
	
	
	public List<Content> getOverviewArticles(long langid,long underid)
	{
		Content c=new Content();
		
		c.setLangId(langid);
		c.setTypeId(6);
		c.setUnderId(underid);
		c.setPagefnum(0);
		c.setPagetnum(10);
		
		return cm.getOverviewArticles(c);
	}
	
	
	
	
	public List<Content> getSearch(long langid, String s)
	{
		Content c=new Content();
		c.setLangId(langid);
		c.setContent(s);
		c.setPagefnum(0);
		c.setPagetnum(20);
		
		return cm.getRearch(c);
	}
}
